
    

function A_Video(dn,obj){
	var A_Video_option = 
	'<div id="fz-videoBox" class="fz-videoBox">'+
		'<div class="fz-loading">'+
			'<div id="bfBtn" class="bfBtn" style="visibility:hidden;opacity:0;">'+
				'<div class="spinner">'+
				  	'<div class="rect1"></div>'+
				  	'<div class="rect2"></div>'+
				  	'<div class="rect3"></div>'+
				  	'<div class="rect4"></div>'+
				  	'<div class="rect5"></div>'+
				'</div>'+
				'<div id="sptxt" class="sptxt">'+
					'msg...'+
				'</div>'+
			'</div>'+
		'</div>'+
		'<video src="" id="fz-videoAct" class="fz-videoAct"></video>'+
		'<div id="videoState" class="videoState">'+
			'<div id="videoStateBox" class="videoStateBox">'+
				'<div id="playOrStop" class="playOrStop">'+
					'<img id="playIcon" width="30px" style=" margin-top: 5px;" src="./img/A_video_zanting.png"></img>'+
				'</div>'+
				'<div id="currentTimeBox" class="currentTimeBox">'+
					'<span id="currentTimers" class="currentTimers">0:00</span>'+
					'<span>/</span>'+
					'<span id="allTimers" class="allTimers">0:00</span>'+
				'</div>'+
				'<div id="percentage" class="percentage">'+
					'<div id="currentPerc" class="currentPerc">'+
					'</div>'+
					'<div id="currentPercentage" class="currentPercentage">'+
					'</div>'+
					'<div id="currentAll" class="currentAll">'+
					'</div>'+
					'<div id="currentBG" class="currentBG">'+
					'</div>'+
				'</div>'+
				'<div id="videoSpeed" class="videoSpeed">'+
					'<div class="videoBoxRe">'+
						'<img id="" width="30px" style=" margin-top: 5px;" src="./img/A_video_beishu.png"></img>'+
						'<ul id="videoSpeedList" class="videoSpeedList">'+
							'<li class="videoSpeedStup" id="bs4">2.5倍</li>'+
							'<li class="videoSpeedStup" id="bs3">2.0倍</li>'+
							'<li class="videoSpeedStup" id="bs2">1.5倍</li>'+
							'<li class="videoSpeedStup" id="bs1">默认</li>'+
						'</ul>'+
					'</div>'+
				'</div>'+
				'<div id="videoSound" class="videoSound">'+
					'<div id="videoSoundBox" class="videoSoundBox">'+
						'<img id="videoSoundStop" width="30px" style=" margin-top: 5px;" src="./img/A_video_yinliang.png"></img>'+
						'<div id="soundBar" class="soundBar">'+
							'<div id="currentSound" class="currentSound">'+
							'</div>'+
						'</div>'+
					'</div>'+
				'</div>'+
				'<div id="quanping" class="quanping"><img width="30px" style="margin-top: 2px;"  src="./img/A_video_quanping.png"></div>'+
			'</div>'+
		'</div>'+
	'</div>';
	if(typeof dn == 'string'){
		this._box = $('#'+dn);
	}else{
		this._box = dn;
	}
	var _box = this._box;
	//节点池
	var fzPools = {

	}

	//点击后停止现实缓冲动画标记
	var hcqStop = 0;

	//获取节点
	this.D		= function(dms){
		var dom = this._box.find('#'+dms);
		return dom[0];
	}
	
	function D(id){
		var dom = _box.find('#'+id);
		return dom[0];
	}
	
	function invokeFieldOrMethod(element, method) {
		var usablePrefixMethod;
		["webkit", "moz", "ms", "o", ""].forEach(function(prefix) {
		if (usablePrefixMethod) return;
		if (prefix === "") {
		// 无前缀，方法首字母小写
		method = method.slice(0,1).toLowerCase() + method.slice(1); 
		}
		var typePrefixMethod = typeof element[prefix + method];
		if (typePrefixMethod + "" !== "undefined") {
		if (typePrefixMethod === "function") {
		usablePrefixMethod = element[prefix + method]();
		} else {
		usablePrefixMethod = element[prefix + method];
		}
		}
		});
		
		return usablePrefixMethod;
	};

	//设置播放器地址
	this.setUrl		= function(url){
		this.D('fz-videoAct').src = url;
	}

	this.timeHide = function(){
		var self = this;
		this._timer = setTimeout(function(){
			console.log(D('videoState'));
			$(D('videoState')).css({opacity:0});
		},5000);
	}
	this.showVideoState = function(){
		var self = this;
		clearInterval(self._timer);
		$(D('videoState')).css({opacity:1});
		this.timeHide();
	}
	//初始化播放器属性
	this.init		= function(){
		//设置播放器地址
		var self = this;
		this.setUrl(obj.url);

		this.videoDOM.controls=false;
		//判断是否自动播放
		if(obj.autoplay){
			this.videoDOM.autoplay = true;
			fzPools.playIcon.src = './img/A_video_play.png';
		} else {
			this.videoDOM.autoplay = false;
			fzPools.playIcon.src = './img/A_video_zanting.png';
		}

		//容器点击事件
		fzPools.VIDEO_DOM.onclick = function(){
			stopOrplay();
//			self.timeHide();
			self.showVideoState();
		}

		//时间改变事件
		fzPools.VIDEO_DOM.ontimeupdate = function(){
			if( fzPools.VIDEO_DOM.paused ){
				fzPools.playIcon.src = './img/A_video_zanting.png';
			} else {
				fzPools.playIcon.src = './img/A_video_play.png';
			}
			fzPools.work.style.width 	= videoCurrentTime() + "%";
			fzPools.work2.style.width 	= videoCurrentTime() + "%";
			fzPools.currentTm.innerHTML = timeConversion(parseInt(fzPools.VIDEO_DOM.currentTime));
			fzPools.allTime.innerHTML 	= timeConversion(parseInt(videoAllTime()));
			if( hcqStop == 0 ){
				var hcq  = parseInt((fzPools.VIDEO_DOM.buffered.end(0) / videoAllTime()).toFixed(2) * 100);
				fzPools.currentBG.style.width = hcq + "%";
			} else {
				fzPools.currentBG.style.width = 100 + "%";
			}
		}

		//缓冲暂停事件
		fzPools.VIDEO_DOM.onwaiting = function(){
			openLoading("on","加载中");
		}

		//缓冲完毕继续播放事件
		fzPools.VIDEO_DOM.onplaying = function(){
			openLoading("off");
		}

		//音量改变事件
		fzPools.VIDEO_DOM.onvolumechange = function(){
			var soundSize 		= fzPools.VIDEO_DOM.volume;
			var soundBarWidth 	= getDomWidth('soundBar') * soundSize;
			fzPools.soundBar.style.width = soundBarWidth + "px";
		}

		//进度条点击事件
		fzPools.percentage.onclick	= function(){
			self.showVideoState();
			var widthCurr 	= mouseCurrentX('percentage')/getDomWidth('percentage');
			fzPools.VIDEO_DOM.currentTime = videoAllTime() * widthCurr;
			hcqStop = 1;
		}
		

		//播放按钮
		fzPools.playOrStop.onclick = function(){
			self.showVideoState();
			if( fzPools.VIDEO_DOM.paused ){
				fzPools.VIDEO_DOM.play();
				fzPools.playIcon.src = './img/A_video_play.png';	
			} else {
				fzPools.VIDEO_DOM.pause();
				fzPools.playIcon.src = './img/A_video_zanting.png';
			}
		}

		//音量调节
		fzPools.soundBarBox.onclick = function(){
			var soundBarWidth 	= getDomWidth('soundBar');
			var clickWidth 		= mouseCurrentX('soundBar');
			fzPools.VIDEO_DOM.volume = clickWidth/soundBarWidth.toFixed(2);
			
		}

		//音量按钮
		fzPools.vSoundStop.onclick = function(){
			if( fzPools.VIDEO_DOM.muted ){
				fzPools.VIDEO_DOM.muted = false;
				fzPools.VIDEO_DOM.volume = 0.99;
			} else {
				fzPools.VIDEO_DOM.muted = true;
				fzPools.VIDEO_DOM.volume = 0;
			}
		}
		//倍速选择
		fzPools.videoSpeed.onclick = function(){
			self.showVideoState();
			
			var ul = $(fzPools.videoSpeedList);
			console.log(ul.css('opacity'));
    		if(ul.css('opacity')==1){
    			ul.css({
	    			opacity: 0
	    		});
    		}else{
    			ul.css({
	  				visibility: 'visible',
	    			opacity: 1
	    		});
    		}
    		
    		
		}
		//倍速按钮
		fzPools.bs1.onclick = function(){
			fzPools.VIDEO_DOM.playbackRate = 1;
		}
		fzPools.bs2.onclick = function(){
			fzPools.VIDEO_DOM.playbackRate = 1.5;
		}
		fzPools.bs3.onclick = function(){
			fzPools.VIDEO_DOM.playbackRate = 2;
		}
		fzPools.bs4.onclick = function(){
			fzPools.VIDEO_DOM.playbackRate = 2.5;
		}
		fzPools.quanping.onclick = function(){

			if(invokeFieldOrMethod(document,'FullScreen') || invokeFieldOrMethod(document,'IsFullScreen') || document.IsFullScreen){
				self.exitFullscreen();
				return;
			}
			var element = fzPools.VIDEO_DOM;
			self.timeHide();
					
			//此方法不可以在異步任務中執行，否則火狐無法全屏
			if(element.requestFullscreen) {
				console.log(1);
				element.requestFullscreen();
			} else if(element.mozRequestFullScreen) {
				console.log(2);
				element.mozRequestFullScreen();
			} else if(element.msRequestFullscreen){ 
				console.log(3);
				element.msRequestFullscreen();
			} else if(element.oRequestFullscreen){
				console.log(4);
				element.oRequestFullscreen();
			}else if(element.webkitRequestFullscreen){
				console.log(5);
				element.webkitRequestFullScreen();
			}else{
				console.log(6);
				var docHtml = document.documentElement;
				var docBody = document.body;
				var videobox = fzPools.VIDEO_DOM;
				var cssText = 'width:100%;height:100%;overflow:hidden;';
				docHtml.style.cssText = cssText;
				docBody.style.cssText = cssText;
				videobox.style.cssText = cssText+';'+'margin:0px;padding:0px;';
				document.IsFullScreen = true;
				
			}
		}
	}

	//创建播放器
	this.createDom  = function(){
		var self = this;
		if( this.D("fz-videoBox") == null ){
			this._box[0].innerHTML = A_Video_option;
			this.videoDOM 			= this.D("fz-videoAct");
			this.saveDom();
			this.init();
		} else {
			console.log("播放器节点已存在");
			return;
		}
	}

	//销毁实例
	this.overVideo = function(){
		var ovDom = this.D(dn);
		var childs = ovDom.childNodes;
		var  length = childs.length;
		for( var i = 0; i < length; i++ ){
			ovDom.removeChild(childs[0]);
		}
	}
	//進入全屏
	this.launchFullscreen = function() 
	{
//		var element = fzPools.VIDEO_DOM;
		
//		// Webkit (works in Safari5.1 and Chrome 15)
//		element.webkitRequestFullScreen();
//		document.webkitCancelFullScreen();
//		
//		// Firefox (works in nightly)
//		element.mozRequestFullScreen();
//		document.mozCancelFullScreen();
		
//		// W3C 提议
//		element.requestFullscreen();
//		document.exitFullscreen();

		console.log(element);
		//此方法不可以在異步任務中執行，否則火狐無法全屏
		if(element.requestFullscreen) {
			element.requestFullscreen();
		} else if(element.mozRequestFullScreen) {
			element.mozRequestFullScreen();
		} else if(element.msRequestFullscreen){ 
			element.msRequestFullscreen(); 
		} else if(element.oRequestFullscreen){
			element.oRequestFullscreen();
		}else if(element.webkitRequestFullscreen){
			element.webkitRequestFullScreen();
		}else{
		
			var docHtml = document.documentElement;
			var docBody = document.body;
//			var videobox = document.getElementById('videobox');
			var videobox = fzPools.VIDEO_DOM;
			var cssText = 'width:100%;height:100%;overflow:hidden;';
			docHtml.style.cssText = cssText;
			docBody.style.cssText = cssText;
			videobox.style.cssText = cssText+';'+'margin:0px;padding:0px;';
			document.IsFullScreen = true;
			
		}
	}
//	退出全屏
	this.exitFullscreen = function(){
		
		if (document.exitFullscreen) {
		document.exitFullscreen();
		} else if (document.msExitFullscreen) {
		document.msExitFullscreen();
		} else if (document.mozCancelFullScreen) {
		document.mozCancelFullScreen();
		} else if(document.oRequestFullscreen){
		document.oCancelFullScreen();
		}else if (document.webkitExitFullscreen){
		document.webkitExitFullscreen();
		}else{
		var docHtml = document.documentElement;
		var docBody = document.body;
		docHtml.style.cssText = "";
		docBody.style.cssText = "";
		fzPools.VIDEO_DOM.style.cssText = "";
		document.IsFullScreen = false;
		}
	}
/*--------------------------------------方法-------------------------------------*/
	//将节点存入节点池
	this.saveDom = function(){
		fzPools.VIDEO_DOM 	= this.D("fz-videoAct");
		fzPools.work 		= this.D("currentPercentage");
		fzPools.work2 		= this.D("currentPerc");
		fzPools.currentTm   = this.D("currentTimers");
		fzPools.allTime 	= this.D("allTimers");
		fzPools.percentage 	= this.D("percentage");
		fzPools.playOrStop	= this.D("playOrStop");
		fzPools.playIcon 	= this.D("playIcon");
		fzPools.bfBtn 		= this.D("bfBtn");
		fzPools.sptxt 		= this.D("sptxt");
		fzPools.soundBarBox = this.D("soundBar");
		fzPools.soundBar  	= this.D("currentSound");
		fzPools.vSoundStop 	= this.D("videoSoundStop");
		fzPools.currentBG   = this.D("currentBG");
		fzPools.bs1 		= this.D("bs1");
		fzPools.bs2 		= this.D("bs2");
		fzPools.bs3 		= this.D("bs3");
		fzPools.bs4 		= this.D("bs4");
		fzPools.quanping 	= this.D("quanping");	
		fzPools.videoSpeed  = this.D("videoSpeed");
		fzPools.videoSpeedList = this.D('videoSpeedList');
	}

	//获取元素的宽度
	function getDomWidth(dmNm){
		var ddmm 	= D(dmNm);
		return parseInt(ddmm.width||ddmm.offsetWidth||ddmm.clientWidth);
	}

	//计算鼠标相对元素X坐标
	function mouseCurrentX(dmNm,event){
		var e = event || window.event;
		var ddmm = D(dmNm);
		var p_Left  = parseInt(ddmm.getBoundingClientRect().left);
		return e.clientX - p_Left;
	}

	//计算鼠标相对元素Y坐标
	function mouseCurrentY(dmNm,event){

	}
	//播放或暂停
	function stopOrplay(state){
		switch (state){
			case "play":
				fzPools.VIDEO_DOM.play();
				break;
			case "stop":
				fzPools.VIDEO_DOM.pause();
				break;
			default:
				if(fzPools.VIDEO_DOM.paused){
					fzPools.VIDEO_DOM.play();
				} else {
					fzPools.VIDEO_DOM.pause();
				}
				break;
		}
	}

	//当前播放进度
	function videoCurrentTime(){
		return (fzPools.VIDEO_DOM.currentTime / videoAllTime()).toFixed(3) * 100;
	}

	//视频总时长
	function videoAllTime(){
		return fzPools.VIDEO_DOM.duration;
	}

	//秒数转时分秒
	function timeConversion(tim){
		var second  = tim % 60;				// 秒
		var min 	= parseInt(tim / 60);	// 分 
		return (Array(2).join(0)+min).slice(-2) + ':' + (Array(2).join(0)+second).slice(-2);
	}

	//打开或关闭加载
	function openLoading(states,msg){
		if( states == "on" ){
			fzPools.bfBtn.style.visibility = "visible";
			fzPools.bfBtn.style.opacity = "1";
			fzPools.sptxt.innerHTML = msg;
		} else {
			fzPools.bfBtn.style.visibility = "hidden";
			fzPools.bfBtn.style.opacity = "0";
			fzPools.sptxt.innerHTML = '';
		}
	}
	this.createDom();
}